# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=quazip
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
         $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6"))
pkgver=1.4
pkgrel=2
pkgdesc="C++ wrapper for the Gilles Vollant's ZIP/UNZIP C package (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://stachenov.github.io/quazip/"
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-qt6-5compat"))
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/stachenov/${_realname}/archive/v${pkgver}.tar.gz")
sha256sums=('79633fd3a18e2d11a7d5c40c4c79c1786ba0c74b59ad752e8429746fe1781dd6')

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DQUAZIP_QT_MAJOR_VERSION=5 \
      -DQUAZIP_FETCH_LIBS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-qt5-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "${srcdir}/build-qt5-${MSYSTEM}"

  if [[ ${CARCH} != i686 ]]; then

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DQUAZIP_QT_MAJOR_VERSION=6 \
      -DQUAZIP_FETCH_LIBS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-qt6-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "${srcdir}/build-qt6-${MSYSTEM}"

  fi
}

package_quazip-qt5() {
  depends+=("${MINGW_PACKAGE_PREFIX}-qt5-base")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "${srcdir}/build-qt5-${MSYSTEM}"

  # Fix cmake files
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f -name '*.cmake' \
    -exec sed -i -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" {} \;

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt5/COPYING"
}

package_quazip-qt6() {
  depends+=("${MINGW_PACKAGE_PREFIX}-qt6-base"
            "${MINGW_PACKAGE_PREFIX}-qt6-5compat")

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "${srcdir}/build-qt6-${MSYSTEM}"

  # Fix cmake files
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f -name '*.cmake' \
    -exec sed -i -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" {} \;

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt6/COPYING"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
